﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace factory
{
    public abstract class Tuxing
    {
        abstract internal double calculateArea();
        abstract internal bool isLegal();


    }

    class Rectangle : Tuxing
    {
        public Rectangle(double a,double b) { this.a = a;this.b = b; }
        public Rectangle() { }
        internal double a;
        internal double b;
        internal static double c = 90.0;
        public double A { get { return a; } set { if (value <= 0) { a = 0; } else { a = value; } } }
        public double B { get { return b; } set { if (value <= 0) { b = 0; } else { b = value; } } }
        internal override double calculateArea()
        {
            if (this.isLegal()) { return a * b; }
            else
            {
                return 0.0;
            }

        }
        internal override bool isLegal()
        {
            if(c==90.0 && a>0 && b > 0) { return true; }
            else { return false; }
        }
    }

    class Square : Rectangle
    {
        public Square(double a):base(a,a) {  }
        internal override bool isLegal()
        {
            if(base.isLegal() && this.a == this.b) { return true; }
            else { return false; }
        }
    }

    class Trangle : Tuxing
    {
        public Trangle(double a,double b, double abAngel) { this.a = a;this.b = b;this.abAngle = abAngel; }
        internal double a;
        internal double b;
        internal double abAngle;
        public double A { get { return a; } set { if (value <= 0) { a = 0; } else { a = value; } } }
        public double B { get { return b; } set { if (value <= 0) { b = 0; } else { b = value; } } }
        internal override double calculateArea()
        {
            return 0.5 * a * b * Math.Sin(abAngle);
        }
        internal override bool isLegal()
        {
            return (abAngle>0 && abAngle < 180.0 && a>0 && b>0);
        }
    }

    public class Factory
    {
        public Factory(){ }
        public Tuxing getTuxing(int x,double a,double b,double c)
        {
            switch (x)
            {
                case 1: { return new Square(a); }
                case 2: { return new Rectangle(a, b); }
                case 3: { return new Trangle(a, b, c); }
                default: { return null; }
            }
        }
        public Tuxing getTuxing(int x ,double a)
        {
            return this.getTuxing(x, a, a, 90.0);
        }
        public Tuxing getTuxing(int x, double a,double b)
        {
            return this.getTuxing(x, a, b, 90.0);
        }
    }
    internal class Program
    {
        static void Main(string[] args)

        {
            double getRandomNumber(double minimum, double maximum, int Len)
            {
                Random random = new Random();
                return Math.Round(random.NextDouble() * (maximum - minimum) + minimum, Len);
            }
            Random r = new Random();
            double sum = 0;
            Factory f = new Factory();
            // 随机摇取类型参数
            for (int i = 0; i < 10; i++)
            {
                Tuxing m=f.getTuxing(r.Next(1, 4), getRandomNumber(0, r.Next(0,100), 5), getRandomNumber(0, r.Next(0,100), 5), getRandomNumber(0, 180, 5));
                if (m.isLegal()) { sum += m.calculateArea();Console.WriteLine(i + ":"+m.GetType()+"'s area=" + m.calculateArea()); }
                else { i -= 1;continue; }
            }
            Console.WriteLine("SUM area=" + sum);
            Console.ReadKey();
        }
    }
}
